<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="联系人姓名或电话号码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status" placeholder="状态" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in types" :key="index" :label="item.name"
                     :value="item.id" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="sizeChangeHandle()">查询</el-button>
        <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="contactName"
        header-align="center"
        align="center"
        label="联系人姓名">
      </el-table-column>
      <el-table-column
        prop="contactPhone"
        header-align="center"
        align="center"
        width="150"
        label="电话号码">
      </el-table-column>
      <el-table-column
        prop="platType"
        header-align="center"
        align="center"
        label="发送平台">
        <template slot-scope="scope">
          <span v-if="scope.row.platType==1">阿里云</span>
          <span v-if="scope.row.platType==2">腾讯云</span>
          <span v-if="scope.row.platType==3">七牛云</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="content"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="短信内容">
      </el-table-column>
      <el-table-column
        prop="messageCode"
        header-align="center"
        align="center"
        width="170"
        label="短信模版">
      </el-table-column>
      <el-table-column
        prop="sendTime"
        header-align="center"
        align="center"
        width="170"
        label="发送时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status==0">未发送</span>
          <span v-if="scope.row.status==1">已发送</span>
          <span v-if="scope.row.status==2">用户接收成功</span>
          <span v-if="scope.row.status==3">用户接收失败</span>
          <span v-if="scope.row.status==4">回复</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详细</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id,scope.row.contactName)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 短信日志详细 -->
    <el-dialog title="短信日志详细" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="联系人姓名："
            >{{ form.contactName }}</el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="电话号码：">{{ form.contactPhone }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发送平台：">
              <span v-if="form.platType==1">阿里云</span>
              <span v-if="form.platType==2">腾讯云</span>
              <span v-if="form.platType==3">七牛云</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模版代码：">{{ form.messageCode }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="操作人：">{{ form.sendUser }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="回执ID：">{{ form.bizId }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发送状态：">
              <span v-if="form.status==0">未发送</span>
              <span v-if="form.status==1">已发送</span>
              <span v-if="form.status==2">用户接收成功</span>
              <span v-if="form.status==3">用户接收失败</span>
              <span v-if="form.status==4">回复</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发送时间：">{{form.sendTime}}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="短信内容：">{{form.content}}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="回执时间：">{{form.reportTime}}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="回复内容：">{{form.replyContent}}</el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          status:''
        },
        types:[
          {id:0,name:'未发送'},
          {id:1,name:'已发送'},
          {id:2,name:'用户接收成功'},
          {id:3,name:'用户接收失败'},
          {id:4,name:'回复'}
        ],

        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        open:false,
        form:{}
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      /** 详细按钮操作 */
      handleView(row) {
        this.open = true;
        this.form = row;
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/smsg/smsgmessagelog/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'status':this.dataForm.status
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 删除
      deleteHandle (id,name) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        var names = name ? [name] : this.dataListSelections.map(item => {
          return item.contactName
        })
        /*this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {*/
        this.$confirm(`确定对[名称=${names.join(',')}]进行[${name ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/smsg/smsgmessagelog/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
